//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CotSwapToFwdAdapter.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (Cephei::QL::Models::Marketmodels::IMarketModel^ coterminalModel) : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
    CMarketModel^ _CcoterminalModel;
    try
    {
#ifdef HANDLE
        _phCotSwapToFwdAdapter = NULL;
#endif
        _CcoterminalModel = safe_cast<CMarketModel^> (coterminalModel);
        _CcoterminalModel->Lock();
        boost::shared_ptr<QuantLib::MarketModel>& _coterminalModel = static_cast<boost::shared_ptr<QuantLib::MarketModel>&> (_CcoterminalModel->GetShared ()); 
        _ppCotSwapToFwdAdapter = new boost::shared_ptr<QuantLib::CotSwapToFwdAdapter> (new QuantLib::CotSwapToFwdAdapter ( _coterminalModel ));
        SetMarketModel (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcoterminalModel != nullptr) _CcoterminalModel->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (boost::shared_ptr<QuantLib::CotSwapToFwdAdapter>& childNative, Object^ owner) : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
#ifdef HANDLE
	_phCotSwapToFwdAdapter = NULL;
#endif
	_ppCotSwapToFwdAdapter = &childNative;
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
}
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (QuantLib::CotSwapToFwdAdapter& childNative, Object^ owner) : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
#ifdef HANDLE
	_phCotSwapToFwdAdapter = NULL;
#endif
	_ppCotSwapToFwdAdapter = new boost::shared_ptr<QuantLib::CotSwapToFwdAdapter> (&childNative);
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
    _CotSwapToFwdAdapterOwner = owner;
    _MarketModelOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (CCotSwapToFwdAdapter^ copy) : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
#ifdef HANDLE
	_phCotSwapToFwdAdapter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCotSwapToFwdAdapter = new boost::shared_ptr<QuantLib::CotSwapToFwdAdapter> (copy->GetShared());
        _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
    }
}
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (PLATFORM::Type^ t) : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
#ifdef HANDLE
	_phCotSwapToFwdAdapter = NULL;
#endif
	if (!t->IsSubclassOf(CCotSwapToFwdAdapter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (QuantLib::Handle<QuantLib::CotSwapToFwdAdapter>& childNative, Object^ owner)  : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
	_phCotSwapToFwdAdapter = &childNative;
	_ppCotSwapToFwdAdapter = &static_cast<boost::shared_ptr<QuantLib::CotSwapToFwdAdapter>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
    _CotSwapToFwdAdapterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (QuantLib::Handle<QuantLib::CotSwapToFwdAdapter> childNative)  : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
	_phCotSwapToFwdAdapter = &childNative;
	_ppCotSwapToFwdAdapter = &static_cast<boost::shared_ptr<QuantLib::CotSwapToFwdAdapter>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::CCotSwapToFwdAdapter (QuantLib::CotSwapToFwdAdapter childNative)  : CMarketModel(CCotSwapToFwdAdapter::typeid)
{
#ifdef HANDLE
	_phCotSwapToFwdAdapter = NULL;
#endif
	_ppCotSwapToFwdAdapter = new boost::shared_ptr<QuantLib::CotSwapToFwdAdapter> (new QuantLib::CotSwapToFwdAdapter (childNative));
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppCotSwapToFwdAdapter));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::~CCotSwapToFwdAdapter ()
{
    if (_ppCotSwapToFwdAdapter != NULL)
    {
	    delete _ppCotSwapToFwdAdapter;
        _ppCotSwapToFwdAdapter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::!CCotSwapToFwdAdapter ()
{
    if (_ppCotSwapToFwdAdapter != NULL)
    {
	    delete _ppCotSwapToFwdAdapter;
    }
}
QuantLib::CotSwapToFwdAdapter& Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::GetReference ()
{
    if (_ppCotSwapToFwdAdapter == NULL) throw REFNEW NativeNullException ();
	return **_ppCotSwapToFwdAdapter;
}
boost::shared_ptr<QuantLib::CotSwapToFwdAdapter>& Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::GetShared ()
{
    if (_ppCotSwapToFwdAdapter == NULL) throw REFNEW NativeNullException ();
	return *_ppCotSwapToFwdAdapter;
}
QuantLib::CotSwapToFwdAdapter* Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::GetPointer ()
{
    if (_ppCotSwapToFwdAdapter == NULL) throw REFNEW NativeNullException ();
	return &**_ppCotSwapToFwdAdapter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CotSwapToFwdAdapter>& Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::GetHandle ()
{
	if (_phCotSwapToFwdAdapter == NULL)
	{
		_phCotSwapToFwdAdapter = new Handle<QuantLib::CotSwapToFwdAdapter> (*_ppCotSwapToFwdAdapter);
	}
	return *_phCotSwapToFwdAdapter;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::HasNative () 
{
	return (_ppCotSwapToFwdAdapter != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::Displacements::get ()
{
    try
    {
    	std::vector<QuantLib::Spread>& _rv = (std::vector<QuantLib::Spread>&)(*_ppCotSwapToFwdAdapter)->displacements ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppCotSwapToFwdAdapter)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::InitialRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCotSwapToFwdAdapter)->initialRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCotSwapToFwdAdapter)->numberOfFactors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCotSwapToFwdAdapter)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCotSwapToFwdAdapter)->numberOfSteps ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter::PseudoRoot (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppCotSwapToFwdAdapter)->pseudoRoot ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::ICotSwapToFwdAdapter^ Cephei::QL::Models::Marketmodels::Models::CCotSwapToFwdAdapter_Factory::Create (Cephei::QL::Models::Marketmodels::IMarketModel^ coterminalModel)
{
    return REFNEW CCotSwapToFwdAdapter ( coterminalModel);
}
